//
//  RankGiftCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/20.
//

import UIKit

class RankGiftCell: UITableViewCell {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.tintColor = .init(named: "red_FE95B4")
        image.wn_cornerWithBorder(4)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var rankImage: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        return image
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.numberOfLines = 2
        label.textColor = .black
        return label
    }()
    
    lazy var giftImage: UIImageView = {
        return UIImageView(image: UIImage(named: "gift_send"))
    }()

    lazy var giftLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "red_FE95B4")
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView(image: UIImage(named: "reward_coin"))
    }()
    
    lazy var goldLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "red_FE95B4")
        return label
    }()
    
    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.showsHorizontalScrollIndicator = false
        scro.showsVerticalScrollIndicator = false
        return scro
    }()
            
    var currentItem = 0 {
        didSet {
            rankImage.image = UIImage(named: "rank_gift_\(currentItem)")
            let w = currentItem > 2 ? 0.0 : (32+Double((3-currentItem))*7.2)
            nameLabel.font = .wn_font(name: .bold, size: CGFloat(currentItem > 2 ? 12 : (14+3-currentItem)))
            giftLabel.font = .wn_font(name: .bold, size: CGFloat(currentItem > 2 ? 12 : (12+3-currentItem)))
            goldLabel.font = .wn_font(name: .bold, size: CGFloat(currentItem > 2 ? 12 : (12+3-currentItem)))
            
            let gw = (currentItem > 2 ? 20 : (28-currentItem*2))
            giftImage.snp.updateConstraints { (make) in
                make.width.height.equalTo(gw)
            }
            let cw = (currentItem > 2 ? 16 : (22-currentItem*2))
            goldImage.snp.updateConstraints { (make) in
                make.width.height.equalTo(cw)
            }
            rankImage.snp.updateConstraints { make in
                make.width.height.equalTo(w)
            }
            bookImage.snp.updateConstraints { (make) in
                make.width.equalTo((self.height-12)*0.677)
            }
            layoutIfNeeded()
        }
    }

    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(model?.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.bookName
                giftLabel.text = "\(m.giftNum)"
                goldLabel.text = "\(m.total)"
                giftArr = m.giftDetial ?? [GiftModel]()
            }
        }
    }
    
    var giftArr = [GiftModel]() {
        didSet {
            for item in scrollView.subviews {
                item.removeFromSuperview()
            }
            var contentW: CGFloat = 0.0
            scrollView.snp.updateConstraints { make in
                make.width.equalTo(44*giftArr.count)
            }
            for i in 0..<giftArr.count {
                let view = RankUserGiftView()
                view.frame = CGRect(x: contentW, y: 0, width: 44, height: 54)
                view.model = giftArr[i]
                contentW += 44
                scrollView.addSubview(view)
            }
            scrollView.contentSize = CGSize(width: contentW, height: 0)
            layoutIfNeeded()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(bookImage)
        contentView.addSubview(rankImage)
        contentView.addSubview(nameLabel)
        contentView.addSubview(giftImage)
        contentView.addSubview(giftLabel)
        contentView.addSubview(goldImage)
        contentView.addSubview(goldLabel)
        contentView.addSubview(scrollView)
        bookImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
            make.top.equalTo(6)
            make.bottom.equalTo(-6)
            make.width.equalTo(0)
        }
        rankImage.snp.makeConstraints { (make) in
            make.left.equalTo(bookImage.snp.left).offset(-12)
            make.top.equalTo(bookImage.snp.top).offset(-6)
            make.width.height.equalTo(0)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bookImage.snp.right).offset(6)
            make.top.equalTo(bookImage.snp.top).offset(4)
            make.right.equalTo(-12)
        }
        giftImage.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.bottom.equalTo(bookImage.snp.bottom).offset(0)
            make.width.height.equalTo(20)
        }
        giftLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(giftImage.snp.centerY)
            make.left.equalTo(giftImage.snp.right).offset(6)
        }
        goldImage.snp.makeConstraints { (make) in
            make.centerX.equalTo(giftImage.snp.centerX)
            make.bottom.equalTo(giftImage.snp.top).offset(-5)
            make.width.height.equalTo(16)
        }
        goldLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(goldImage.snp.centerY)
            make.left.equalTo(giftLabel.snp.left)
        }
        scrollView.snp.makeConstraints { (make) in
            make.centerY.equalTo(goldImage.snp.centerY)
            make.right.equalTo(-8)
            make.width.equalTo(0)
            make.height.equalTo(54)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class RankUserGiftView: UIView {
    
    lazy var giftImage: UIImageView = {
        return UIImageView()
    }()
        
    lazy var countLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 10)
        label.textAlignment = .center
        label.textColor = .black
        return label
    }()

    var model: GiftModel? {
        didSet {
            if let m = model {
                giftImage.wn_setupImg(m.cover, placeHolder: "gift_send")
                countLabel.text = "x\(m.num)"
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(giftImage)
        addSubview(countLabel)
        giftImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.width.height.equalTo(40)
        }
        countLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(giftImage.snp.bottom).offset(2)
            make.left.equalTo(0)
            make.right.equalTo(0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
